<?php
namespace app\SuperAdmin\controller;
use think\Db;
use think\Controller;
use think\Session;
class UsersController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('adminid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {   
        $user=db("users")
        ->order('uid asc')
        ->paginate(10);
        $this->assign('list',$user);
        return $this->fetch('users');
    }

     public function delete(){
       $uid=input('id');
       $users=Db::name("users")->find($uid);
       $pic=$users["upic"];
       $file=ROOT_PATH."public".DS."static".DS. 'img'.DS.'userimg'.DS.$pic;
       if (file_exists($file)) {
        unlink($file);
       }
       if (Db::table("users")->delete($uid)){
         $this->success("删除成功");
       }
       else{
         $this->error("删除失败"); 
       }
    }

    public function show(){
      $uid=input('uid');
      $list=Db::name('users')->where('uid',$uid)->find();
      echo(json_encode($list)) ;
    }
    
    public function search(){
        $a=input('content');  
        if($a) {
        $sql=Db::table('users')
        ->where('uname','like','%'.$a.'%')
        ->paginate(5);
     $this->assign('list',$sql);
         }
         else{
           $user=db("users")
        ->order('uid asc')
        ->paginate(5);
        $this->assign('list',$user);
         }
     return $this->fetch('users');
    }

}